
import string
import findcdelimiter as fcd

def txt_lookup(data):
    ''' turn text to list-of-lists (to preserve items order)
        rows starting with # are ignored

    import pepe.p_txt.txt_lookup as ptl
    ptl.txt_lookup(data)
    '''
    lines = string.split(data,'\n')
    res = []; cd = ""

    for kv in lines:
        #kv = string.strip(kv)
        if string.strip(kv) <> '' and string.strip(kv)[0:1] <> '#':
            if cd == "": cd = fcd.fcd(kv)
            #kv = string.strip(kv)
            if kv <> '':
                res.append(string.split(kv, cd))
    return res

def txt_lookupfile(ifile):
    ''' turn text in file to list-of-lists (to preserve items order)
        rows starting with # are ignored

    import pepe.p_txt.txt_lookup as ptl
    ptl.txt_lookupfile(ifile)
    '''
    lines = open(ifile,'r').read()
    return txt_lookup(lines)
    
if __name__ == '__main__':
    ''' '''
    lkp = """
    # dsfdffs
    # dsfs

    A|1
    B|2
    C|3
    """
    print '\n from text %s\n%s%s \n' % ('- '*20, lkp, txt_lookup(lkp))

    print '\n from file %s\n%s \n' % ('- '*20, txt_lookupfile('example_txt_lookupfile.txt'))
